
package com.dy.template;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.dy.model.Column;
import com.dy.model.Creator;
import com.dy.model.TableInfo;

import cn.dy.util.FileUtil;

/**
 * @Description:
 * @Date：2018年6月29日 @author： DongY
 * @version ：1.0 1.[2018年6月29日]CreatBy：dy
 */

public class Model implements BaseSting {

	@Override
	public String getStr(final TableInfo tableInfo, final Creator c) {
		final List<Column> columns = tableInfo.getColumns();
		String classString = "";
		try {
			classString = FileUtil.readAsString(c.getTemplatePath());
		} catch (final IOException e) {

			e.printStackTrace();
			System.exit(0);
		}
		String imp = "";

		final Set<String> set = tableInfo.getImportSet();
		final Iterator<String> its = set.iterator();
		while (its.hasNext()) {
			final String string = its.next();
			imp += "\r\nimport " + string + ";";
		}
		classString = classString.replace("###imports###", imp);
		String string = "";

		for (final Column column : columns) {
			if (column.isPRI() && column.getJavaName().equals(tableInfo.getPriKeyColumn())) {
				string += "\r\n\t@Id";
			}
			string += "\r\n\t@ApiModelProperty(value = \"" + column.getRemark() + "\")";
			if (column.isIscrement()) {
				string += "\r\n\t@GeneratedValue(strategy = GenerationType.IDENTITY)";
			}
			string += "\r\n\tprivate " + column.getJavaType().getSimpleName() + " " + column.getJavaName() + ";\r\n";
		}
		classString = classString.replace("###feilds###", string);
		return classString;
	}

}
